#include "environment.h"

environment::environment()
{
        env = new std::map<int,EnvironmentInfo*>();
}
void environment::showInfo()
{
    for(auto kkk = env->begin(); kkk!=env->end();kkk++)
    {
        qDebug() << kkk->first << "," << kkk->second->fileName;
    }
}
void environment::newEnvironment(int index,EnvironmentInfo *newEnv)
{
        EnvironmentInfo*ev = new EnvironmentInfo();
        (*ev).operator=(newEnv);
        env->insert(std::make_pair(index,ev));
}
void environment::updateEnvironment(int index,EnvironmentInfo *upEnv)
{
    EnvironmentInfo *tarEnv = (*env)[index];
    tarEnv->pic = upEnv->pic;
    *(tarEnv->tmp) = *(upEnv->tmp);
    tarEnv->mode = upEnv->mode;
    tarEnv->pmap = upEnv->pmap;
    tarEnv->poly = upEnv->poly;
    tarEnv->offset = upEnv->offset;
    *(tarEnv->tempPt) = *(upEnv->tempPt);
    tarEnv->picSize = upEnv->picSize;
    tarEnv->fileName = upEnv->fileName;
    tarEnv->imgOffset = upEnv->imgOffset;
    tarEnv->lastPoint = upEnv->lastPoint;
    tarEnv->isDragging = upEnv->isDragging;
    tarEnv->polyOffset = upEnv->polyOffset;
    tarEnv->origPicSize = upEnv->origPicSize;
    tarEnv->realPicSize = upEnv->realPicSize;
    tarEnv->scaleFactor = upEnv->scaleFactor;
    tarEnv->currMousePos = upEnv->currMousePos;
    tarEnv->isCompleteEdit = upEnv->isCompleteEdit;
    *(tarEnv->regionTmp) = *(upEnv->regionTmp);
    tarEnv->currImg = upEnv->currImg;
    //20240808
    tarEnv->rotateFactor = upEnv->rotateFactor;
    tarEnv->centerPos = upEnv->centerPos;
}
EnvironmentInfo* environment::selectEnvironment(int index)
{
    return (*env)[index];
}
void environment::delEnvironment(int index)
{
    std::map<int,EnvironmentInfo*> *tempEnv = new std::map<int,EnvironmentInfo*>();
    std::vector<int> keysToRemove;
    if(containsEnvironment(index))
    {
            keysToRemove.push_back(index);
    }
    for(auto i =env->begin();i!=env->end();i++)
    {
            if(i->first > index)
            {
                int num = i->first - 1;
                if(containsEnvironment(i->first))
                {
                    EnvironmentInfo *currEnv = new EnvironmentInfo();
                    currEnv->operator=(i->second);
                    tempEnv->insert(std::make_pair(num,currEnv));
                    keysToRemove.push_back(i->first);
                }
            }
    }
        //env->erase(index);
    if(!keysToRemove.empty())
        {
            for(int i : keysToRemove)
            {
                delete  (*env)[i];
                (*env)[i] = NULL;
                env->erase(i);
            }
            if(!tempEnv->empty())
            {
                for(auto j = tempEnv->begin();j != tempEnv->end();j++)
                {
                    env->insert(*j);
                }
            }
        }
}
bool environment::containsEnvironment(int index){
    return (env->find(index) != env->end());
}

std::map<int,EnvironmentInfo*>* environment::getEnvironmentLists()
{
    return env;
}
